package com.lushu.chapitre1.graphics.controls
{
    import com.lushu.chapitre1.events.MenuEvent;
    import com.sfdk.graphics.core.TextFieldEx;
    import com.sfdk.graphics.ui.parts.ComponentBase;
    import com.sfdk.locale.LocalisedMessageEngine;
    import com.sfdk.pools.BitmapDataPool;
    import com.sfdk.pools.MusicPool;

    import flash.display.Bitmap;
    import flash.events.Event;

    public class ConfigurableMenu extends ComponentBase
    {
        private var _menuContainer:Bitmap;

        private var _menuInfoBackground:Bitmap;
        private var _menuInfoTextField:TextFieldEx;
        private var _buttonConfiguration:Vector.<Object>;

        private var _northButton:Bitmap;
        private var _northButton2:Bitmap;
        private var _northButtonObject:Object;

        private var _leftButton:Bitmap;
        private var _leftButton2:Bitmap;
        private var _leftButtonObject:Object;

        private var _rightButton:Bitmap;
        private var _rightButton2:Bitmap;
        private var _rightButtonObject:Object;

        private var _downButton:Bitmap;
        private var _downButton2:Bitmap;
        private var _downButtonObject:Object;

        private var _initialBtnName:String;
        private var _scale:Number;

        private var _currentFrame:int;
        private var _targetYPosition:int;
        private var _hideAfterwards:Boolean;
        private var _currentBtnDefinition:Object;
        private var _currentBtn:Bitmap;

        public static const MAGIC_BTN:Object = {name :"MAGIC_BTN", image : "resources/images/magic", event : MenuEvent.MAGIC};
        public static const ITEM_BTN:Object = {name :"ITEM_BTN", image : "resources/images/item", event : MenuEvent.ITEM};
        public static const ATTACK_BTN:Object = {name :"ATTACK_BTN", image : "resources/images/attack", event : MenuEvent.ATTACK};
        public static const STAY_BTN:Object = {name :"STAY_BTN", image : "resources/images/stay", event : MenuEvent.STAY};
        public static const MEMBER_BTN:Object = {name :"MEMBER_BTN", image : "resources/images/member", event : MenuEvent.MEMBER};
        public static const SEARCH_BTN:Object = {name :"SEARCH_BTN", image : "resources/images/search", event : MenuEvent.SEARCH};


        public function ConfigurableMenu(scale:Number) {
            _scale = scale;
            super();
        }

        public function configure(menuConfiguration:Vector.<Object>, initialBtn:String) : void {
            _buttonConfiguration = menuConfiguration;
            _leftButtonObject = menuConfiguration[0];
            _northButtonObject = menuConfiguration[1];
            _rightButtonObject = menuConfiguration[2];
            _downButtonObject = menuConfiguration[3];
            _initialBtnName = initialBtn;
            refresh();
        }

        public function refresh():void {
            this.removeAllChildren();
            createChildren();
            createView(null);
        }

        override protected function createChildren():void {
            super.createChildren();
            if (_buttonConfiguration != null) {

                _menuContainer = BitmapDataPool.instance.getAsBitmap("resources/images/menuContainer.png");
                _northButton = BitmapDataPool.instance.getAsBitmap(_northButtonObject.image + ".png");
                _northButton.scaleX = _scale;
                _northButton.scaleY = _scale;

                _northButton2 = BitmapDataPool.instance.getAsBitmap(_northButtonObject.image + "2.png");
                _northButton2.scaleX = _scale;
                _northButton2.scaleY = _scale;
                _northButton2.visible = false;

                _leftButton = BitmapDataPool.instance.getAsBitmap(_leftButtonObject.image + ".png");
                _leftButton.scaleX = _scale;
                _leftButton.scaleY = _scale;

                _leftButton2 = BitmapDataPool.instance.getAsBitmap(_leftButtonObject.image + "2.png");
                _leftButton2.scaleX = _scale;
                _leftButton2.scaleY = _scale;
                _leftButton2.visible = false;

                _rightButton = BitmapDataPool.instance.getAsBitmap(_rightButtonObject.image + ".png");
                _rightButton.scaleX = _scale;
                _rightButton.scaleY = _scale;

                _rightButton2 = BitmapDataPool.instance.getAsBitmap(_rightButtonObject.image + "2.png");
                _rightButton2.scaleX = _scale;
                _rightButton2.scaleY = _scale;
                _rightButton2.visible = false;

                _downButton = BitmapDataPool.instance.getAsBitmap(_downButtonObject.image + ".png");
                _downButton.scaleX = _scale;
                _downButton.scaleY = _scale;

                _downButton2 = BitmapDataPool.instance.getAsBitmap(_downButtonObject.image + "2.png");
                _downButton2.scaleX = _scale;
                _downButton2.scaleY = _scale;
                _downButton2.visible = false;
                _menuInfoBackground = BitmapDataPool.instance.getAsBitmap("resources/images/menuInfo.png");
                _menuInfoTextField = new TextFieldEx("SFSys-12");

                setTargetButton(_initialBtnName);
            }

        }

        override protected function mapHotKeys():void {
            super.mapHotKeys();
        }

        override protected function cleanView(e:Event):void {
            super.cleanView(e);
        }

        override protected function createView(e:Event):void {
            super.createView(e);
            if (_buttonConfiguration != null) {

                attachChild(_menuContainer, 0, 0, "menuContainer");

                attachChild(_leftButton, 0, 24, "leftButton");
                attachChild(_leftButton2, 0, 24, "leftButton2");

                attachChild(_northButton, 2 * 24, 0, "northButton");
                attachChild(_northButton2, 2 * 24, 0, "northButton2");

                attachChild(_rightButton, 4 * 24, 24, "rightButton");
                attachChild(_rightButton2, 4 * 24, 24, "rightButton2");

                attachChild(_downButton, 2 * 24, 48, "downButton");
                attachChild(_downButton2, 2 * 24, 48, "downButton2");

                attachChild(_menuInfoBackground, 160, 48, "menuInfo");
                attachChild(_menuInfoTextField, 160 + 16, 48 + 14);
            }
        }

        override public function update():void {
            super.update();
            if (this.y != _targetYPosition) {
                if (this.y < _targetYPosition)
                    this.y += 30;
                else
                    this.y -= 30;
            }
            else {
                if (_hideAfterwards)
                    this.visible = false;
            }
            if (_currentBtn && _currentFrame <= 0) {
                _currentBtn.visible = !_currentBtn.visible;
                _currentFrame = 14;
            }
            else {
                _currentFrame--;
            }
        }

        private function resetAnimation():void {
            _northButton2.visible = false;
            _downButton2.visible = false;
            _rightButton2.visible = false;
            _leftButton2.visible = false;
            _currentFrame = 14;
        }


        public function hide():void {
            setTargetY(448, true);
        }

        public function show():void {
            setTargetY(448 - 24 * 5);
        }

        public function setTargetY(yPos:int, hideAfterwards:Boolean = false):void {
            this.visible = true;
            MusicPool.getFromPool("resources/sounds/Menu Pop.mp3").play();
            _targetYPosition = yPos;
            _hideAfterwards = hideAfterwards;
        }

        public function setTargetButton(buttonName:String):void {
            resetAnimation();
            for each(var buttonDefinition:Object in _buttonConfiguration) {
                if (buttonDefinition.name == buttonName) {
                    _menuInfoTextField.text = LocalisedMessageEngine.getMessage(buttonDefinition.name);
                    _currentBtnDefinition = buttonDefinition;
                    switch (_currentBtnDefinition) {
                        case _northButtonObject:
                            _currentBtn = _northButton2;
                            break;

                        case _downButtonObject:
                            _currentBtn = _downButton2;
                            break;

                        case _leftButtonObject:
                            _currentBtn = _leftButton2;
                            break;

                        case _rightButtonObject:
                            _currentBtn = _rightButton2;
                            break;
                    }
                    break;
                }
            }
        }

        /**
         *  UI Handlers
         */

        public function handleArrowKeys(e:Event):void {
            var targetBtn:Bitmap;
            var targetBtnDefinition:Object;
            var targetText:String;
            switch (e.type) {
                case "LEFT":
                    targetBtnDefinition = _leftButtonObject;
                    targetBtn = _leftButton2;
                    targetText = LocalisedMessageEngine.getMessage(_leftButtonObject.name);
                    break;

                case "RIGHT":
                    targetBtnDefinition = _rightButtonObject;
                    targetBtn = _rightButton2;
                    targetText = LocalisedMessageEngine.getMessage(_rightButtonObject.name);
                    break;

                case "UP":
                    targetBtnDefinition = _northButtonObject;
                    targetBtn = _northButton2;
                    targetText = LocalisedMessageEngine.getMessage(_northButtonObject.name);
                    break;

                case "DOWN":
                    targetBtnDefinition = _downButtonObject;
                    targetBtn = _downButton2;
                    targetText = LocalisedMessageEngine.getMessage(_downButtonObject.name);
                    break;
            }
            if (_currentBtnDefinition != targetBtn) {
                MusicPool.getFromPool("resources/sounds/Menu Move.mp3").play();
                resetAnimation();
                targetBtn.visible = true;
                _currentBtn = targetBtn;
                _currentBtnDefinition = targetBtnDefinition;
                _menuInfoTextField.text = targetText;
            }
        }

        public function handleEnterKey(e:Event):void {
            MusicPool.getFromPool("resources/sounds/Menu Confirm.mp3").play();
            dispatchEvent(new MenuEvent(_currentBtnDefinition.event));
            hide();
        }
    }
}
